#!/bin/sh

#####################################################
#### This script automatically builds and
#### installs libraries and header files for ATLAS and CLAPACK
#####################################################


#####################################################
### VARIABLES you need to customise
#####################################################

## BUILD_DIR: Where you want to "build". Default: /tmp
BUILD_DIR="/tmp"

## Where the libraries should go. Default: /usr/lib
LIB_DEST_DIR="/usr/lib"

## Where the include files should go. Default: /usr/include
INCLUDE_DEST_DIR="/usr/include"

## URL to download the atlas tar.gz file.
ATLAS_URL="http://aleron.dl.sourceforge.net/sourceforge/math-atlas/atlas3.6.0.tar.gz"

## URL to download the clapack.tgz file
CLAPACK_URL="http://www.netlib.org/clapack/clapack.tgz"

## URL to download the clapack header file (clapack.h)
CLAPACK_HEADER_URL="http://www.dnanalytics.net/files/clapack.h"

#######################################################

do_download() {
        URL=$1
        OUTPUT_FILE=$2

        echo "Downloading ${OUTPUT_FILE}..."
        if [ ! -f ${OUTPUT_FILE} ]; then
                wget ${URL} -O ${OUTPUT_FILE}
        else
                echo "No need to download. File already exists"
        fi
}

atlas_download() {
        do_download ${ATLAS_URL} atlas.tar.gz
}

atlas_extract() {
        tar xzf atlas.tar.gz
        cd ATLAS
}

atlas_get_proctype() {
        PROC_TYPE=`ls Make.[^t]* | awk -F"." '{print $2}'`
}

atlas_make_install() {
        make install arch=${PROC_TYPE}
}

clapack_download() {
        wget ${CLAPACK_URL} -O clapack.tar.gz
        wget ${CLAPACK_HEADER_URL} -O clapack.h
}

clapack_extract() {
        tar xzf clapack.tar.gz
        cd CLAPACK
}

clapack_post_extract() {
        cp INSTALL/make.inc.LINUX ./make.inc
        sed -e 's/^BLASLIB.*/BLASLIB = ..\/..\/libcblaswr.a -L..\/ATLAS\/Linux_P4SSE2\/lib -lcblas -latlas/g' make.inc > make.inc.new
        mv make.inc.new make.inc
}

clapack_make() {
        make f2clib
        make cblaswrap
        cd SRC
        make
        cd ..
}

clapack_combine() {
        mkdir -p combine
        cd combine
        ar x ../../ATLAS/lib/${PROC_TYPE}/liblapack.a
        ar r ../lapack_LINUX.a *.o
        cd ..
        mv lapack_LINUX.a liblapack.a
}

clapack_copy_libs() {
        cp  ../ATLAS/lib/${PROC_TYPE}/*.a ${LIB_DEST_DIR}
        cp liblapack.a ${LIB_DEST_DIR}
        cp libcblaswr.a ${LIB_DEST_DIR}
        cp F2CLIBS/*.a ${LIB_DEST_DIR}
}

clapack_copy_include() {
        cp F2CLIBS/libF77/f2c.h ${INCLUDE_DEST_DIR}
        cp ../ATLAS/include/cblas.h ${INCLUDE_DEST_DIR}
        cp ../clapack.h ${INCLUDE_DEST_DIR}
}



main() {
        cd ${BUILD_DIR}

        rm -rf ATLAS CLAPACK

        atlas_download
        atlas_extract
        make
        atlas_get_proctype
        atlas_make_install

        cd ..

        clapack_download
        clapack_extract

        clapack_post_extract
        clapack_make
        clapack_combine
        clapack_copy_libs
        clapack_copy_include
}

 ## Run the main program
main

